using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Customer;
using System.Collections;
using System.Net.Sockets;
using System.Net;


namespace WindowsFormsApplication1
{
    public partial class UsernamePassword : Form
    {
        Person person;
        BitArray status;
        string cardnumber;
        string username;
        string password;
        string ipAddress = "10.40.136.93";//"10.40.163.142";//"10.40.167.6";//"10.40.137.203";//"10.254.254.253";
        int portNumber = 9050;
        bool allLetters = false;
        bool allNumbers = false;
        bool keepChecking = true;
        bool keepChecking1 = true;
        bool correctLength = false;

        // for checking username and password
        string tempUsername;
        string tempPassword;

        byte[] data = new byte[1024];
        IPEndPoint ipep;
        Socket server;

        string drinkOrderRevised;
        byte[] data2 = new byte[1024];
        IPEndPoint Sender;
        EndPoint Remote;
        int recv;
        string message;
        string stringData; //drink is going to be the variable from the user interface

        public UsernamePassword(Person Person, BitArray Status)
        {
            person = Person;
            status = Status;
            InitializeComponent();
            this.elementHost1.Child = new SingerOSK.SingerOnScreenKeyboard(this.Handle);

        }

        // send the data to the bartender interface to save the new username and password
        private void saveButton_Click(object sender, EventArgs e)
        {
            tempUsername = usernameTextBox.Text;
            tempPassword = passwordTextbox.Text;

            // check to make sure every character in password is letter            
            if (tempUsername.Length > 0)
            {
                if ((!isLetters(tempUsername)))
                {
                    allLetters = false;
                }
                else
                {
                    allLetters = true;
                }

                if ((!isInteger(tempPassword)) | (tempPassword.Length != 4))
                {
                    allNumbers = false;
                }
                else {
                    allNumbers = true;
                }

                if (allLetters & allNumbers)
                {
                    person.Password = passwordTextbox.Text;
                    person.Username = usernameTextBox.Text;

                    // get the track, username, and password to send to the bartender
                    cardnumber = person.CardNumber;
                    username = person.Username;
                    password = person.Password;

                    //set up a new connection
                    ipep = new IPEndPoint(IPAddress.Parse(ipAddress), portNumber);
                    server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    drinkOrderRevised = "$" + cardnumber + "#" + username + "#" + password + "#";
                    data = Encoding.ASCII.GetBytes(drinkOrderRevised);

                    // try to send data over the new connection
                    try
                    {
                        server.SendTo(data, data.Length, SocketFlags.None, ipep);
                    }
                    catch (SocketException ex)
                    {
                        message = ex.Message;
                        System.Windows.Forms.MessageBox.Show(message);
                    }
                    // set up a connection to receive data  
                    Sender = new IPEndPoint(IPAddress.Any, 0);
                    Remote = (EndPoint)Sender;
                    // try to receive the data     
                    try
                    {
                        recv = server.ReceiveFrom(data2, ref Remote);
                        stringData = Encoding.ASCII.GetString(data2, 0, recv);
                        System.Windows.Forms.MessageBox.Show(stringData);
                    }
                    catch (SocketException ex)
                    {
                        message = ex.Message;
                        System.Windows.Forms.MessageBox.Show(message + " \nYou will not be charged for this order");
                        stringData = "No Host Detected";
                        MessageBox.Show(stringData);
                    }

                    this.Close();
                }
                else
                {
                    if (!allLetters)
                    {
                        System.Windows.Forms.MessageBox.Show("Username Must Be All Letters, Try Again");
                        allLetters = true;
                    }
                    else if (!allNumbers)
                    {
                        System.Windows.Forms.MessageBox.Show("Password must be 4 numbers");
                        allNumbers = true;
                    }
                }
            }
            else
            {
                MessageBox.Show("Please Enter A Username");
            }
        }

        private void helpButton_Click(object sender, EventArgs e)
        {
            string helpMessage = "Please select a username that consists of only characters " +
              "and a password that consists of four numbers.  After setting up a username and " +
              " password, you may download the AutoBev phone application for your Droid and order " +
              "drinks directly from your phone.  Your credit card information will be stored in " +
              " our database for charging purposes.";
            HelpForm helpForm = new HelpForm(helpMessage);
            helpForm.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        public bool isLetters(String str)
        {
            for (int i = 0; i < str.Length; i++)
            {
                int Ascii = (int)str[i];
                if (Ascii < 65 || Ascii > 122)
                { // check if it not a letter
                    return false;
                }
            }
            return true;  // if all letters
        }

        public bool isInteger(String i)
        {
            try
            {
                Convert.ToInt16(i); 
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        

    }
}
